// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// G [author] rhyme_lph
// c [email]  rhymelph@gmail.com
// b [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/format/service_and_product_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/page/dashboard/products_services/quantity_page.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';
import 'package:flutter/material.dart';

class ItemAmountProvider extends BaseProvider {
  TextEditingController amountController = TextEditingController();
  FocusNode amountFocusNode = FocusNode();

  int itemID;

  String itemName;

  String itemType;

  SettingDataTax gstRate;

  SettingDataCurrency currency;

  bool isCheckInclusive = false;

  bool isShowDelete = false;

  bool get enable =>
      amountController.text.length > 0 && gstRate != null && currency != null;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    amountController.dispose();
    amountFocusNode.dispose();
  }

  void unFocus() {
    amountFocusNode.unfocus();
  }

  @override
  Future get dataFuture => null;

  void onNavDone() {
    ProductEntity product;
    ServicesEntity service;
    if (itemType == 'product') {
      product = ProductEntity()
        ..name = itemName
        ..id = itemID
        ..price = num.parse(amountController.text)
        ..tax = Config.currentCompany.enableGst == 1 ? gstRate : Config.noneTax
        ..isTaxInclusive = isCheckInclusive ? 1 : 0;
    } else {
      service = ServicesEntity()
        ..name = itemName
        ..id = itemID
        ..price = num.parse(amountController.text)
        ..tax = Config.currentCompany.enableGst == 1 ? gstRate : Config.noneTax
        ..isTaxInclusive = isCheckInclusive ? 1 : 0;
    }

    unFocus();
    if (isShowDelete == true) {
      obtainContext?.call((context) {
        Navigator.of(context)
          ..pop(ServiceAndProductEntity()
            ..number = 1
            ..product = product
            ..services = service);
      });
    } else {
      obtainContext?.call((context) {
        Navigator.of(context)
          ..pop(
            ServiceAndProductEntity()
              ..number = 1
              ..product = product
              ..services = service,
          );
      });
    }
  }

  void onSelectGstRate() {
    unFocus();
    amountFocusNode.unfocus();
    obtainContext?.call((context) async {
      gstRate = await showGstRateBottomSheet(context, gstRate);
      update();
    });
  }

  void onChangeInclusive() {
    unFocus();
    isCheckInclusive = !isCheckInclusive;
    update();
  }

  String getGSTInfo() {
    double amount = double.parse(amountController.text, (e) => 0.0);
    double rateDouble = gstRate.rate / 100;
    if (isCheckInclusive == true) {
      double sourceAmount = amount / (1 + rateDouble);
      double inclusive = amount - sourceAmount;
      return CashUtils.formatCashAmount(inclusive);
    } else {
      double gst = amount * rateDouble;
      return CashUtils.formatCashAmount(gst);
    }
  }

  String getTotal() {
    double amount = double.parse(amountController.text, (e) => 0.0);
    double rateDouble = gstRate.rate / 100;
    if (isCheckInclusive == true) {
      return CashUtils.formatCashAmount(amount);
    } else {
      double result = amount + amount * rateDouble;
      return CashUtils.formatCashAmount(result);
    }
  }

  void onDelete() {
    obtainContext?.call((context) {
      Navigator.of(context)
        ..pop({
          'delete': true,
        });
    });
  }

  void initData(ServiceAndProductEntity item, {bool isEdit = false}) {
    this.itemID = item.id;
    this.itemName = item.name;
    this.itemType = item.type;
    print("${this.itemType}");
    this.currency = currency ?? Config.defaultCurrency;
    this.gstRate = Config.settingInfo.taxData;
    this.isShowDelete = isEdit ?? false;
    if (item.tax != null) {
      this.gstRate = item.tax;
    }
    this.isCheckInclusive = item.isInclusive;
    if (item.amount != null) {
      this.amountController =
          TextEditingController(text: item.totalAmount.toStringAsFixed(2));
    } else {
      amountController = TextEditingController();
    }
    amountController.addListener(() {
      update();
    });
  }
}
